package com.rongchuang.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.AssociationDTO;
import com.rongchuang.entity.Association;
import com.rongchuang.entity.QAssociation;
import com.rongchuang.repository.AssociationRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AssociationService {

    @Autowired
    private AssociationRepository associationRepository;

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * 保存商会信息
     */
    public void modify(AssociationDTO dto) {
        Association entity;
        if (Objects.isNull(dto.getId())) {
            entity = ConverterUtil.convertTo(dto, Association.class);
            entity.setCreateTime(DateUtils.getCurrentTime());
            entity.setStatus((byte) 1);
        }else {
            entity = associationRepository.getById(dto.getId());
            if (StringUtils.isNotBlank(dto.getName())) {
                entity.setName(dto.getName());
            }
            if (StringUtils.isNotBlank(dto.getIntroduction())) {
                entity.setIntroduction(dto.getIntroduction());
            }
            if (StringUtils.isNotBlank(dto.getContactPerson())) {
                entity.setContactPerson(dto.getContactPerson());
            }
            if (StringUtils.isNotBlank(dto.getContactPhone())) {
                entity.setContactPhone(dto.getContactPhone());
            }
            if (Objects.nonNull(dto.getStatus())) {
                entity.setStatus(dto.getStatus());
            }
        }
        entity.setUpdateTime(DateUtils.getCurrentTime());
        associationRepository.save(entity);
    }

    /**
     * 分页查询
     */
    public Page<AssociationDTO> pageSearch(int pageNum, int pageSize, String name) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QAssociation association = QAssociation.association;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(name)) {
            builder.and(association.name.like("%" + name + "%"));
        }
        List<Association> associations = queryFactory
                .selectFrom(association)
                .where(builder)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(association.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(association)
                .where(builder)
                .fetchCount();
        List<AssociationDTO> associationDTOS = ConverterUtil.convertToList(associations, AssociationDTO.class);
        return new PageImpl<>(associationDTOS, pageable, total);
    }

    /**
     * 获取商会详情
     * @param id 商会id
     */
    public AssociationDTO getDetail(Long id) {
        Association association = associationRepository.getById(id);
        return ConverterUtil.convertTo(association, AssociationDTO.class);
    }
}
